package com.wheesper.www.WheesperServer.Common;

import java.util.HashMap;
import java.util.Map;
import java.util.Queue;
import com.Wheesper.www.WheesperServer.Model.User;
import com.wheesper.www.WheesperServer.Services.LogController;
import com.wheesper.www.WheesperServer.Utils.Logger;

import ProtocolBuffer.ProtoMessageOuterClass.ProtoMessage;
import java.util.ArrayList;


public class OfflineMessageTrusteeship {
	private static OfflineMessageTrusteeship _instance = new OfflineMessageTrusteeship();
	public static OfflineMessageTrusteeship Instance() {
		return _instance;
	}
	
	private Map<User,ArrayList<ProtoMessage> > _trusteeship;
	
	private OfflineMessageTrusteeship() {
		_trusteeship = new HashMap<User,ArrayList<ProtoMessage>>();
		//TODO:Nothing
	}
	
	//检查是否有属于自己的寄托消息
	public boolean hasMine(User user) {
		return _trusteeship.containsKey(user);
	}
	
	
	//取出寄存在托管所里面的所有消息
	public ArrayList<ProtoMessage> pickUpMine(User user){
		Logger.Instance().logging("Taking Offline Message User:" + user.eMail());
		ArrayList<ProtoMessage> messagesQueue = _trusteeship.get(user);
		_trusteeship.remove(user);
		return messagesQueue;
	}
	
	//任何用户都可以使用这个方法来给其他用户寄托消息
	public void trusteeFor(User receiver,ProtoMessage message) {
		ArrayList<ProtoMessage> messagesQueue;
		if((messagesQueue = _trusteeship.get(receiver)) != null){
			messagesQueue.add(message);
		}else {
			messagesQueue = new ArrayList<ProtoMessage>();
			messagesQueue.add(message);
			_trusteeship.put(receiver, messagesQueue);
		}
		
		Logger.Instance().logging("Trustee a Offline Message for User:" + receiver.eMail());
	}
}
